using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy3DTurretBoss : EnemyBaseFixed
{
    private SeqAction workAction=new(){beginWaitTime=60f/60f,intervalTime=10f/60f};
    private SeqAction idleAction=new(){beginWaitTime=60f/60f,intervalTime=10f/60f};

    private Sprite[] shellSprites;
    private Sprite closeSprite;
    private Sprite openSprite;
    private Transform shellObj;
    private Transform flaskBackObj;

    private Sprite residualSprite;

    private bool needDestroy=false;
    void Awake()
    {
        base.Init();
        shellObj=transform.Find("壳");
        flaskBackObj=transform.Find("FlashBack");
        bulletPrefab=Constant.Prefabs.EnemyBulletM.Load();

        shellSprites=new Sprite[]{
            ResourcesPool.Sprites["魂-第二关-Boss-闭"],
            ResourcesPool.Sprites["魂-第二关-Boss-炮-半开"],
            ResourcesPool.Sprites["魂-第二关-Boss-炮-开"],
        };
        closeSprite=ResourcesPool.Sprites["魂-第二关-Boss-闭"];
        openSprite=ResourcesPool.Sprites["魂-第二关-Boss-炮-开"];

        residualSprite=ResourcesPool.Sprites["魂-第二关-Boss-残"];
        fireAction=new(){intervalTime=80f/60f};

        shellObj.gameObject.SetActive(true);
        flaskBackObj.gameObject.SetActive(false);
        mainSR.enabled=false;
        collider2D.enabled=true;

    }
    void Start()
    {
        Stop();
    }
    private void FixedUpdate() {
        base.CommonFixedUpdate();
    }

    protected void UpdateStateNone(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState=EnumState.Idle;
    }
    protected void UpdateStateIdle(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState=inState;
        if(stateChanged){
            idleAction.ReStart();
        }
        
        EnumState tempState=inState;
        idleAction.Update((SeqAction seq)=>{
            shellObj.gameObject.SetActive(true);
            if(seq.seqNo>shellSprites.Length){
                seq.done=true;
                shellObj.gameObject.SetActive(false);
                tempState=EnumState.Working;
            }else{
                if(shellObj.TryGetComponent(out SpriteRenderer sr)){
                    sr.sprite=shellSprites[seq.seqNo-1];
                }
            }
        });
        outState=tempState;

    }
    protected void UpdateStateWorking(bool stateChanged, EnumState inState, out EnumState outState){
        outState=inState;
        if(stateChanged){
            Fire();
            workAction.ReStart();
        }
        EnumState tempState=inState;
        workAction.Update((SeqAction seq)=>{
            shellObj.gameObject.SetActive(true);
            if(seq.seqNo>shellSprites.Length){
                seq.done=true;
                tempState=EnumState.Idle;
            }else{
                if(shellObj.TryGetComponent(out SpriteRenderer sr)){
                    sr.sprite=shellSprites[shellSprites.Length-seq.seqNo];
                }
            }
        });
        outState=tempState;
        
    }
    protected override void UpdateStateDie(bool stateChanged, EnumState inState, out EnumState outState){
        base.UpdateStateDie(stateChanged,inState,out outState);
        if(stateChanged){
            CreateBoom(transform.position);
            Constant.Sounds.EnemyDeath2.PlayOneShot();
            if(TryGetComponent(out SpriteRenderer sr)){
                sr.sprite=residualSprite;
            }
            shellObj.gameObject.SetActive(false);
        }

        if(needDestroy){
            Destroy(gameObject);
        }

    }

    public override float Damage(GameObject source, float value)
    {
        if(!running || !(state==EnumState.Working)){
            return value;
        }else{
            return base.Damage(source, value);
        }
    }

    private void Fire(){
        float angleGap=30f;
        int count=3;
        float offsetAngle=(count-1)*angleGap/2;
        for(int i=0;i<count;i++){
            Quaternion angle=Quaternion.AngleAxis(angleGap*i-offsetAngle,Vector3.forward);
            Vector2 direction=angle*Vector3.down;
            if(Instantiate(bulletPrefab,transform.position,Quaternion.identity).TryGetComponent(out ABullet bullet)){
                bullet.Launch(direction);
            }
        }
    }

    public override void Run()
    {
        if(!running){
            shellObj.gameObject.SetActive(true);
            flaskBackObj.gameObject.SetActive(true);
            mainSR.enabled=true;
            collider2D.enabled=true;
            running = true;
        }
    }

    public override void Stop()
    {
        if (running)
        {
            shellObj.gameObject.SetActive(true);
            flaskBackObj.gameObject.SetActive(false);
            mainSR.enabled=false;
            collider2D.enabled=false;
            running = false;
        }
    }

    public override void DieAndDestroy()
    {
        needDestroy=true;
        Die();
    }
}
